package com.android.readcard.server;

import android.nfc.Tag;
import android.nfc.tech.NfcB;

import com.android.readcard.utils.djlm.DJLMReader3.reader.HexUtil;

import java.io.IOException;

/**
 * Created by Admin on 2017/4/9.
 */
public class NFCReaderService {
    private NfcB m_nfcB = null;
    /////////////////////////////////////////////////////
    static NFCReaderService mNFCReaderService = null;
    public static NFCReaderService GetNFCReaderService()
    {
        if (mNFCReaderService == null)
            mNFCReaderService = new NFCReaderService();
        return mNFCReaderService;
    }
    //////////////////////////////////
    public void ProcessTag(Tag tagFromIntent)
    {
        boolean bTypeB = false;
        String resultStr = "";
        for (String tech : tagFromIntent.getTechList()) {
            resultStr += tech + "\r\n";
            System.out.println(tech);
            if (tech.equals("android.nfc.tech.NfcB")) {
                bTypeB = true;
            }
        }
        if (bTypeB) {
            System.out.println("getId:" + HexUtil.bytesToHexString(tagFromIntent.getId()));
            NfcB nfcB = NfcB.get(tagFromIntent);
            System.out.println("ApplicationData:" + HexUtil.bytesToHexString(nfcB.getApplicationData()));
            System.out.println("ProtocolInfo:" + HexUtil.bytesToHexString(nfcB.getProtocolInfo()));
            System.out.println("MaxTransceiveLength:" + nfcB.getMaxTransceiveLength());
            try {
                nfcB.connect();
            } catch (IOException e) {
                e.printStackTrace();
            }
            SetNfcB(nfcB);
        }
    }
    //界面线程设置NfcB对象的接口，界面发现卡片后，会调用此接口
    public void SetNfcB(NfcB nfcB)
    {
        synchronized(this) {
            m_nfcB = nfcB;
            System.out.println("Set NFC 1.0");
            long nBeginTime = System.currentTimeMillis();
            //while(System.currentTimeMillis() < nBeginTime + 20000)
            {
                if (m_nfcB == null) {
                    System.out.println("NFC break");
                    //break;
                }
            }
        }
        //System.out.println("NFC Time out");
        /*
        try {
            Tag tag = nfcB.getTag();
            System.out.println("ProtocolInfo:" + bytesToHexString(nfcB.getProtocolInfo()));
            System.out.println("ApplicationData:" + bytesToHexString(nfcB.getApplicationData()));
            System.out.println("id:" + bytesToHexString(tag.getId()));
            byte[] send = {0x50, 0x00,0x00, 0x00, 0x00,0x00, 0x00};
            System.out.println("send:" + bytesToHexString(send));
            byte[] Ret = nfcB.transceive(send);
            System.out.println("rev:" + bytesToHexString(Ret));
        } catch (android.nfc.TagLostException e) {
            System.out.println("lost tag");
            m_nfcB = null;
        } catch (IOException e)
        {
            m_nfcB = null;
        }
        */
    }

    NfcB GetNfcB()
    {
        NfcB nfcB;
        synchronized(this) {
            if (m_nfcB == null) {
                System.out.println("NFC break");
            }
            nfcB = m_nfcB;
        }
        return nfcB;
    }

    //根据nfcB对象，与卡片通讯
    byte [] m_idleOP = new byte[]{0x05, 0x00, 0x00};
    public int NFCTrans(byte[] nfcQuery, byte[] retData) throws IOException
    {
        byte[] Ret = null;
        int nRetLen = -1;
        if (nfcQuery != null) {
            byte[] query = nfcQuery;
            System.out.println("NFC send:" + HexUtil.bytesToHexString(query));
            long nBeginTime = System.currentTimeMillis();
            NfcB nfcB = GetNfcB();
            if (nfcB != null) {
                if (nfcB.isConnected()) {
                    try {
                        try {
                            //System.out.println("NFC send:"+HexUtil.bytesToHexString(Query));
                            Ret = nfcB.transceive(query);
                        } catch (android.nfc.TagLostException e) {
                            System.out.println("lost tag");
                            nfcB = null;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        //throw e;
                    }
                    if (Ret != null) {
                        if (query[0] == 0x05) {
                            Ret = new byte[]{0x50, 0x00, 0x00, 0x00, 0x00, (byte) 0xD1, 0x03, 0x00, (byte) 0x81, 0x00, 0x70, (byte) 0xC0};
                            if (retData.length >= 12) {
                                System.arraycopy(Ret, 0, retData, 0, Ret.length);
                                //System.arraycopy(nfcB.getApplicationData(), 0, retData, 5, 4);
                                //System.arraycopy(nfcB.getProtocolInfo(), 0, retData, 9, 3);
                                nRetLen = 12;
                            }
                            else
                                nRetLen = 0;
                        } else if (query[0] == 0x1D) {
                            Ret = new byte[]{0x08};
                            if (retData.length >= 1) {
                                System.arraycopy(Ret, 0, retData, 0, Ret.length);
                                nRetLen = 1;
                            }
                        } else if (Ret.length > 1) {
                            System.arraycopy(Ret, 0, retData, 0, Ret.length - 1);
                            nRetLen = Ret.length - 1;
                        }
                    }
                }
            }
            else
            {
                try{
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println("NFC rev::("+(System.currentTimeMillis() - nBeginTime)+")" + HexUtil.bytesToHexString(Ret));
        }
        else
        {
			/*
            NfcB nfcB = GetNfcB();
            if (nfcB != null) {
                if (nfcB.isConnected()) {
                    try {
                        try {
                            //System.out.println("NFC send:"+HexUtil.bytesToHexString(Query));
                            nfcB.transceive(m_idleOP);
                            System.out.println("nfc idle!");
                        } catch (android.nfc.TagLostException e) {
                            System.out.println("lost tag");
                            nfcB = null;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        //throw e;
                    }
                }
            }
			*/
        }
        if (nRetLen == -1 && nfcQuery != null && nfcQuery.length > 0 && nfcQuery[0] == 0x05)
        {
            nRetLen = 0;
        }
        return nRetLen;
    }
}
